/*
 * Copyright 2010-2012 Susanta Tewari. <freecode4susant@users.sourceforge.net>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package commons.util;

import java.util.Properties;

import javax.xml.bind.PropertyException;

/**
 * @author Susanta Tewari
 * @version 1.0 Aug 11, 2010
 */
public class PropertyUtil {

    /**
     * Constructs ...
     *
     */
    private PropertyUtil() {}

    /**
     * A property is deemed valid if it is not <code>null</code> and has a non-empty value.
     *
     * @param prop property
     * @param value value of the property
     * @deprecated Use {@link ValidationUtil#validateProperty(java.util.Properties,
     *             java.lang.String)} instead
     */
    @SuppressWarnings("TypeMayBeWeakened")
    public static void validateProperty(String prop, String value) {

        if ((value == null) || (value.length() == 0)) {
            throw new RuntimeException("property " + prop + "=" + value);
        }
    }


    /**
     * Validates that the key exists in the specified properties object and has a non-empty value.
     *
     * @param props a properties object used in the validation
     * @param key the property key being validated
     * @throws PropertyException if the key does not exist or has an empty value
     * @deprecated Use {@link ValidationUtil#validateProperty(java.util.Properties,
     *             java.lang.String)} instead
     */
    public static void validateProperty(Properties props, String key) throws PropertyException {

        if (props.getProperty(key) == null) {
            throw new PropertyException("Property " + key + "does not exist.");
        }

        if (props.getProperty(key).isEmpty()) {
            throw new PropertyException("Property " + key + "is empty.");
        }

    }


    /**
     * @param dest destination properties
     * @param src source properties
     */
    public static void loadProperties(Properties dest, Properties src) {

        for (String key : src.stringPropertyNames()) {

            dest.setProperty(key, src.getProperty(key));
        }
    }


    /**
     * Creates a new instance.
     *
     * @param props properties to be copied
     * @return a new copy of the specified properties instance
     */
    public static Properties copyProperties(Properties props) {

        Properties result = new Properties();

        for (String key : props.stringPropertyNames()) {

            result.setProperty(key, props.getProperty(key));
        }

        return result;
    }
}
